<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------

/**
 * Request_model class
 *
 * This class abstracts functions for the photo requests
 *
 * @package		Photoggle
 * @subpackage	Models
 * @category	Models
 * @author		Russ
 */
class Request_model extends CI_Model {

	/* Request bahavior */
	var $id   = '';
	var $request_info_id = '';
	var $title = '';
	var $description = '';
	var $category_id = '';

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * Fetch all open requests which are not completed
	 *
	 * @access	public
	 * @return	array
	 */
	function get_open_requests()
	{
		$sql = "
				SELECT
					pt_requests.id AS requestId,
					pt_requests.title AS requestTitle,
					pt_requests.description AS requestDescription,
					pt_requests.price AS requestPrice,
					pt_requests.status AS requestStatus,
					pt_requests.category_id AS requestCategoryId,
					pt_requests.date_due AS requestDateDue,
					pt_requests.date_created AS requestDateCreated,
					pt_requests.user_id AS requestUserId,
					pt_users.username AS userUsername,
					pt_categories.name AS categoryName,
					(
						SELECT
							COUNT(*)
						FROM
							pt_request_comments
						WHERE
							pt_request_comments.request_id=pt_requests.id
					) AS requestNumComments
				FROM
					pt_requests
				JOIN
					pt_users ON pt_users.id=pt_requests.user_id
				JOIN
					pt_categories ON pt_categories.id=pt_requests.category_id
				WHERE
					pt_requests.status=0
				ORDER BY 
					pt_requests.date_created
				DESC
				LIMIT
					30";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	/**
	 * Fetch the number of open requests which are not completed with the option of filtering by category
	 *
	 * @access	public
	 * @param	id (optional)
	 * @return	array
	 */
	function get_num_open_requests()
	{
		$this->db->select('1', FALSE);
		$this->db->where('status', 0);
		$query = $this->db->get('pt_requests');
		return $query->num_rows();
	}
	
	/**
	 * Inserts a new request object
	 *
	 * @access	public
	 * @return	boolean
	 */
	function insert_entry()
	{
		$this->request_info_id = $_POST['request_info_id'];
		$this->title = $_POST['title'];
		$this->description = $_POST['description'];
		$this->category_id = $_POST['category_id'];
		
		// Insert new category
		$this->db->insert('pt_request_info', $this);
		// TODO: Return true or false on success/failure
	}

	/**
	 * Updates a request object
	 *
	 * @access	public
	 * @return	boolean
	 */
	function update_entry()
	{
		// TODO
	}

 }
 ?>